package com.jeasonzhao.report.engine.impl;

import com.jeasonzhao.commons.logger.Logger;
import com.jeasonzhao.commons.xml.XMLNode;
import com.jeasonzhao.report.exceptions.ChartException;
import com.jeasonzhao.report.model.Report;
import com.jeasonzhao.report.model.chart.Chart;
import com.jeasonzhao.report.model.chart.ChartTypes;
import com.jeasonzhao.report.model.chart.DataPoint;
import com.jeasonzhao.report.model.chart.DataPointAry;
import com.jeasonzhao.report.model.chart.DataSeries;

public class VisiFireRender extends AbstractChartHtmlRender
{
    public VisiFireRender()
    {
    }

    public void teardown(Logger log,Report report)
        throws ChartException
    {

    }

    public void setup(Logger log,Report report)
        throws ChartException
    {
        writeln("<script type=\"text/javascript\"" +
                " src=\"./Visifire2.js\"></script>");
    }

    protected String getChartRenderAs(ChartTypes c)
    {
        if(null == c)
        {
            return null;
        }
        else
        {
            String str = c.getConstantFieldName();
            if(str.endsWith("Chart"))
            {
                str = str.substring(0,str.length() - "Chart".length());
            }
            return str;
        }
    }

    protected void renderChart(Logger log,Chart chart,String strDivID)
    {
        XMLNode nodeChart = new XMLNode("vc:Chart");
        nodeChart
            .addAttribute("xmlns:vc","clr-namespace:Visifire.Charts;assembly=SLVisifire.Charts")
            .addAttribute("Watermark","False")
            .addAttribute("View3D","True")
            .addAttribute("Padding","2")
            .addAttribute("Width","%WIDTH%")
            .addAttribute("Height","%HEIGHT%")
            ;
        if(chart.getTitle() != null)
        {
            nodeChart
                .addNode("vc:Chart.Titles")
                .addNode("vc:Title")
                .addAttribute("Text",chart.getTitle());
        }
        XMLNode nodeChartSeries = nodeChart.addNode("vc:Chart.Series");
        for(int nds = 0;nds < chart.getDataSeries().size();nds++)
        {
            DataSeries dataSeries = chart.getDataSeries().get(nds);
            for(int nd = 0;null != dataSeries && null != dataSeries.getDataPointMatrix() && nd < dataSeries.getDataPointMatrix().size();nd++)
            {
                DataPointAry datas = dataSeries.getDataPointMatrix().get(nd);
                XMLNode nodeDataSeries = nodeChartSeries.addNode("vc:DataSeries");
                nodeDataSeries
                    .addAttribute("RenderAs",getChartRenderAs(dataSeries.getChartType()))
                    .addAttribute("LegendText",dataSeries.getTitle())
                    .addAttribute("AxisYType",dataSeries.getAxisType())
                    ;
                XMLNode nodeDataPoints = nodeDataSeries.addNode("vc:DataSeries.DataPoints");
                for(int ndv = 0;null != datas && ndv < datas.size();ndv++)
                {
                    DataPoint ele = datas.get(ndv);
                    if(ele == null)
                    {
                        continue;
                    }
                    nodeDataPoints.addNode("vc:DataPoint")
                        .addAttribute("AxisXLabel",ele.getAxisLabel())
                        .addAttribute("YValue",ele.getValue());
                }
            }
        }

        writeln("<script language=\"javascript\" type=\"text/javascript\">");
        writeln("\t" + "var ___targetElement=document.getElementById(\"" + strDivID + "\");");
        writeln("\t" + "___targetElement.style.border=0;___targetElement.style.padding=0;___targetElement.style.margin=0;");
        writeln("\t" + "var ___width=___targetElement.offsetWidth?___targetElement.offsetWidth:500;");
        writeln("\t" + "var ___height=___targetElement.offsetHeight?___targetElement.offsetHeight:200;");
        writeln("\t" + "var vChart = new Visifire2(\"c:/SL.Visifire.Charts.xap\");");
        String strX = nodeChart.toHtml()
            .replaceAll("\"%WIDTH%\"","\"'+___width+'\"")
            .replaceAll("\"%HEIGHT%\"","\"'+___height+'\"");
        writeln("\t" + "vChart.setDataXml('" + strX + "');");
        writeln("\t" + "vChart.render(\"" + strDivID + "\");");
        writeln("</script>");
    }
}
